# ******************************************************************
#
# Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os
import os.path
from tools.scons.RunTest import run_test

Import('test_env')

# SConscript file for Local PKI google tests
srmtest_env = test_env.Clone()
target_os = srmtest_env.get('TARGET_OS')
rd_mode = srmtest_env.get('RD_MODE')

######################################################################
# Build flags
######################################################################
with_upstream_libcoap = srmtest_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
    srmtest_env.AppendUnique(CPPPATH=['#extlibs/libcoap/libcoap/include'])
else:
    srmtest_env.AppendUnique(CPPPATH=['../../connectivity/lib/libcoap-4.1.1/include'])

srmtest_env.PrependUnique(CPPPATH=[
    '../../../c_common/oic_malloc/include',
    '../../connectivity/inc',
    '../../connectivity/inc/pkix',
    '../../connectivity/api',
    '../../connectivity/external/inc',
    '../include/internal',
    '../../logger/include',
    '../../include',
    '../../stack/include',
    '../../stack/include/internal',
    '../../../oc_logger/include',
    '../provisioning/include',
    '../include'
])

srmtest_env.PrependUnique(LIBS=[
    'octbstack_internal',
    'ocsrm',
    'routingmanager',
    'oc_logger_internal',
    'connectivity_abstraction_internal',
    'coap'
])

if srmtest_env.get('SECURED') == '1':
    srmtest_env.AppendUnique(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

if srmtest_env.get('LOGGING') == '1':
    srmtest_env.AppendUnique(CPPDEFINES=['TB_LOG'])

if srmtest_env.get('MULTIPLE_OWNER') == '1':
    srmtest_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])

if target_os == 'windows':
    srmtest_env.AppendUnique(LIBS=['sqlite3'])
    #Disable deprecated code warning we are still testing deprecated functions till they are removed.
    srmtest_env.AppendUnique(CCFLAGS=['/wd4996'])
else:
    # TODO: Implement feature check.
    srmtest_env.AppendUnique(CPPDEFINES=['HAVE_LOCALTIME_R'])
    #Disable deprecated code warning we are still testing deprecated functions till they are removed.
    srmtest_env.AppendUnique(CFLAGS=['-Wno-attributes'])
    if target_os in ['linux', 'tizen']:
        srmtest_env.ParseConfig('pkg-config --cflags --libs sqlite3')
        srmtest_env.ParseConfig('pkg-config --cflags --libs gobject-2.0 gio-2.0 glib-2.0')

######################################################################
# Source files and Targets
######################################################################
unittests = []
unittests += srmtest_env.Program('unittest', [
    'aclresourcetest.cpp',
    'amaclresourcetest.cpp',
    'pstatresource.cpp',
    'doxmresource.cpp',
    'policyengine.cpp',
    'securityresourcemanager.cpp',
    'credentialresource.cpp',
    'spresource.cpp',
    'srmutility.cpp',
    'iotvticalendartest.cpp',
    'base64tests.cpp',
    'pbkdf2tests.cpp',
    'srmtestcommon.cpp',
    'crlresourcetest.cpp'
])

# this path will be passed as a command-line parameter,
# so needs encoding to avoid problems with escapes on Windows
unittest_build_dir = Dir('.').abspath + os.sep
try:                   # if success, this will be Python 2.x
    unittest_build_dir = unittest_build_dir.encode('string_escape')
    unittest_build_dir = unittest_build_dir.replace("\\", "\\\\")
except LookupError:    # this will be Python 3.x - unicode strings
    unittest_build_dir = unittest_build_dir.encode('unicode_escape')
    unittest_build_dir = str(unittest_build_dir).replace("\\", "\\\\")
srmtest_env.AppendUnique(CPPDEFINES=[
    'SECURITY_BUILD_UNITTEST_DIR=' + unittest_build_dir
])

unittests += srmtest_env.ScanJSON('resource/csdk/security/unittest')

srmtest_env.AppendTarget('test')
if srmtest_env.get('TEST') == '1':
    if target_os in ['linux', 'windows']:
        out_dir = srmtest_env.get('BUILD_DIR')
        result_dir = os.path.join(srmtest_env.get('BUILD_DIR'), 'test_out') + os.sep
        if not os.path.isdir(result_dir):
            os.makedirs(result_dir)
        srmtest_env.AppendENVPath('GTEST_OUTPUT', ['xml:' + result_dir])
        srmtest_env.AppendENVPath('LD_LIBRARY_PATH', [out_dir])
        run_test(srmtest_env,
                 'resource_csdk_security_unittest.memcheck',
                 'resource/csdk/security/unittest/unittest')

srmtest_env.UserInstallTargetExtra(unittests, 'tests/resource/csdk/security/')

